mainApp.factory("empLeaveFactory",function(){

	var service={};
	var object;
	service.setEmp=function(data){
		object=data;

	}
	service.getEmp=function(){
		return object;
	}
	service.getListData=function(currentindex){
		var obj={
			"pageIndex": currentindex,
			"pageSize": 6
		}
		var list = "";
		RequestUtils.postJson('../employee/getByStatus/0',obj).done(function (resp) {
			console.log(resp);
			if(resp.resultCode==0){
				console.log(resp.data);
				list=resp.data;
				CommonUtils.buildFailHint("列表获取","成功");
			}else{
				CommonUtils.buildFailHint('列表获取', '失败');
			}
		});
		return list;
	}

	service.getAllByEmpIdOrName=function(jobnum,name){

		var list = "";
		RequestUtils.getJson('../employee/get/0/' + jobnum + '/' + name).done(function (resp) {
			console.log(resp);
			if(resp.resultCode==0){
				list=resp.data;
				CommonUtils.buildFailHint("查询数据","成功");
			}else{
				CommonUtils.buildFailHint('查询数据', '失败');
			}
		});

		console.log(list);
		return list;
	}
	service.getEmpById=function(id){
		var list = "";
		$.ajax({

			url: '../employee/getById/' + id,
			type: 'GET',
			async: 'true',
			cache: 'no-cache',
			dataType: 'json',
			async: false,//同步
			success: function (result) {
				list=result.data;
			}
		})
		return list;
	}

	service.UpdateEmp=function(emp){
		console.log(emp);
		RequestUtils.postForm('../employee/update', emp)
			.done(function (resp) {
				console.log(resp);
				if(resp.resultCode==0){
					alert("修改成功");

				}else{
					alert("失败")
				}
			});
	};

	//查询所有service
	service.selectAll=function(){
		var list="";
		RequestUtils.getJson('../employee/getAll').done(function (resp) {
			console.log(resp);
			if(resp.resultCode==0){
				list=resp.data;
				CommonUtils.buildFailHint("查询数据","成功");
			}else{
				CommonUtils.buildFailHint('查询数据', '失败');
			}
		});
		return list;
	};

	return service;
})

mainApp.controller("empLeaveListController",function($scope,$http,$state,empLeaveFactory){
	$scope.toUpdate=function(index){
		//console.log( $scope.emplist[index]);
        var tab={url:"empLeave-update",text:"员工详情",id:"empLeave-update-tab",closeable:1};
        $scope.$emit('addTab', tab);
        empLeaveFactory.setEmp($scope.emplist[index]);
		$state.go('empLeave-update');
	}

	//获取数据 全部的
	$scope.list=empLeaveFactory.getListData(1);
	$scope.emplist=$scope.list.items;

	//查询数据 姓名或者工号
	$scope.empWhere='';
	$scope.selectEmpByWhere=function(){
		var name,jobNum;

		if($("#selectEmpJobnum").val()==''||$("#selectEmpJobnum").val()==null){
			jobNum='null'
		}else {
			jobNum=$("#selectEmpJobnum").val();
		}
		if($("#selectEmpName").val()==''||$("#selectEmpName").val()==null){
			name='null';
		}else {
			name=$("#selectEmpName").val();
		}
		$scope.emplist=empLeaveFactory.getAllByEmpIdOrName(jobNum,name);

	}


	//翻页
	$scope.NextListData=function(pageIndex){
		//分页后的数据
		$scope.list=empLeaveFactory.getListData(pageIndex);
		console.log($scope.list);
		//每一页的数据
		$scope.emplist=$scope.list.items;


	}
})

mainApp.controller("empLeaveUpdateController",function($scope,$http,$state,empLeaveFactory,departFactory){

	var emp=empLeaveFactory.getEmp();
	$scope.UpdateEmp=empLeaveFactory.getEmpById(emp.id);
	console.log($scope.UpdateEmp);
	$scope.departList=departFactory.selectAll();
	var list=$scope.departList;
	for (var i=0;i<list.length;i++)
	{
		if(depart.name==list[i].name){
			console.log(list[i]);
		}
	}
})


//mainApp.controller("empLeaveAddController",function($scope,$http,empLeaveFactory,departFactory){
//	//在部门处处查询的所有部门信息
//	$scope.departList=departFactory.selectAll();
//	$scope.depart;
//	$scope.addEmpData;
//	$scope.updateDepart=function(depart){
//		if(depart.name!='null'){
//			$scope.addEmpData
//		}
//		console.log(depart);
//		var list=$scope.departList;
//		for (var i=0;i<list.length;i++)
//		{
//			if(depart.name==list[i].name){
//				console.log(list[i]);
//			}
//		}
//
//	}
//
//	console.log($scope.depart);
//	console.log($scope.departList[0]);
//	console.log($scope.departList);
//	$scope.addEmp=function(emp,departObj){
//		var empObj = {
//			'number': emp.number,
//			'jobnum': emp.jobnum,
//			'name': emp.name,
//			'depart.id': departObj.id,
//			'post': emp.post,
//			'phone': emp.phone,
//			'status': emp.status,
//			'information':emp.information
//		};
//		console.log(empObj);
//		//增加
//        empLeaveFactory.addEmp(empObj);
//
//	}
//
//})